package com.qf.ebook.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyInterceptor implements HandlerInterceptor {
    // jdk1.8中接口的新特性


    @Override
    // 权限校验，可以拦截当前请求，不执行controller
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("执行了MyInterceptor的preHandle");
        return true;  // 返回true,代表放行
    }

    @Override
    // 当controller正常返回信息时，会执行当前方法，修改ModelAndView
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("执行了MyInterceptor的postHandle");
    }

    @Override
    // 无论controller执行失败与否，都会执行当前方法，获取异常信息，记录日志
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("执行了MyInterceptor的afterCompletion");
    }
}
